$(function () {

    // 填充个人信息
    var userId = Cookies.get("userId");
    if(userId > 0){
        $.ajax({
            type:"get",
            url: "/work/getProfile?userId=" + userId,
            dataType: "json",
            success: function (result) {
                fillData(result);
            },
            error: function (){
                $("#warning-fade").html("服务请求失败！");
                $("#myModal").modal();
            }
        });
    }

    // 提交个人信息
    $("#resume-submit").click(function () {
        var fullName = $("#full-name").val();
        var gender = $('#sex-select option:selected').val();
        var birthday = $("#date-of-birth").val();
        var mobile = $("#phone").val();
        var email = $("#e-mail").val();
        var major = $("#major").val();
        var grade = $("#grade-select option:selected").val();
        var additionalInfo = $("#addition-information").val();

        if(fullName == ''){
            $("#full-name-warning").html("姓名不能为空！");
            return;
        }

        if(birthday == ''){
            $("#birthday-warning").html("出生年月不能为空！");
            return;
        }

        if(mobile == ''){
            $("#mobile-warning").html("手机号不能为空！");
            return;
        }
        var regular = /^[0-9]{10,12}$/;
        if(!regular.exec(mobile)){
            $("#mobile-warning").html("手机号码为11位数字！");
            return;
        }

        if(major == ''){
            $("#mobile-warning").html("专业不能为空！");
            return;
        }

        $.ajax({
            type:"post",
            url: "/work/createProfile",
            contentType: "application/json",
            dataType: "json",
            data: JSON.stringify({
                "userId": userId,
                "fullName": fullName,
                "birthday": birthday,
                "gender": gender,
                "phoneNumber": mobile,
                "email": email,
                "grade": grade,
                "major": major,
                "additionalInfo": additionalInfo
            }),
            success: function (result) {
                dealResult(result);
            },
            error: function (){
                $("#warning-fade").html("服务请求失败！");
                $("#myModal").modal();
            }
        });
    });

    function dealResult(result) {
        if(result.statusNum == 200){
            window.location.href="index.html";
        }
        if(result.statusNum == 504){
            $("#submit-warning").html(result.statusInfo);
        }
    }

    function fillData(data) {
        $("#full-name").val(data.data.fullName);
        $("#sex-select option[value='男']").prop("selected", false);
        $("#sex-select").find("option:contains('" + data.data.gender + "')").prop("selected", true);

        $("#date-of-birth").val(data.data.birthday);
        $("#phone").val(data.data.phoneNumber);
        $("#e-mail").val(data.data.email);
        $("#major").val(data.data.major);
        $("#grade-select option[value='大一']").prop("selected", false);
        $("#grade-select").find("option:contains('" + data.data.grade + "')").prop("selected", true);

        $("#addition-information").html(data.data.additionalInfo);
    }
});